package com.smartum.mpr.client.event;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartum.mpr.client.services.UserService;
import com.smartum.mpr.client.services.UserServiceAsync;
import com.smartum.mpr.client.utils.HistoryHelper;
import com.smartum.mpr.client.utils.Utils;
import com.smartum.mpr.client.view.MaskPanel;

/**
 * Fires when user clicks on <code>logout</code> button.
 * <p/>
 * During this action {@link com.smartum.mpr.client.services.UserService#logout()} should be called, because it
 * perform necessary action for session cleanup.
 */
public class LogoutButtonHandler implements ClickHandler {
    public void onClick(ClickEvent event) {

        final MaskPanel maskPanel = new MaskPanel();
        Utils.hideLogoImage();
        maskPanel.show();

        final UserServiceAsync userServiceAsync = GWT.create(UserService.class);
        userServiceAsync.logout(new AsyncCallback<Void>() {
            public void onFailure(Throwable caught) {
                maskPanel.hide();
                // TODO: error screen should be implemented
            }

            public void onSuccess(Void result) {
                maskPanel.hide();
                HistoryHelper.navigateToken(HistoryHelper.LOGIN_TOKEN);
            }
        });
    }
}
